package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhSettleMapper;
import com.zjdiepu.www.ibiz.dh.IDhSettleService;
import com.zjdiepu.www.model.dh.dto.DhSettle;
import com.zjdiepu.www.model.dh.ext.DhSettleExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年05月17日 10时39分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhSettleServiceImpl extends BaseService implements IDhSettleService {
	public static final Logger logger = LoggerFactory.getLogger(DhSettleServiceImpl.class);

	@Autowired
	private DhSettleMapper dhSettleMapper;
	
	@Override
	public void saveUpdateAndDelete(DhSettle dhSettle, OperateType operType) {
		switch (operType) {
		case add:
			dhSettle.initInsertData(dhSettle.getModifier(), dhSettle.getModifierName());
			dhSettleMapper.insert(dhSettle);
			break;
		case edit:
			dhSettle.initUpdateData(dhSettle.getModifier(), dhSettle.getModifierName());
			// 影响的记录数
			int updateCount = dhSettleMapper.updateByPrimaryKeySelective(dhSettle);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhSettle.initDeleteData(dhSettle.getModifier(), dhSettle.getModifierName());
			// 影响的记录数
			int deleteCount = dhSettleMapper.updateByPrimaryKeySelective(dhSettle);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhSettle> selectByExt(DhSettleExt dhSettleExt) {
		return dhSettleMapper.selectByExt(dhSettleExt);
	}
	
	@Override
	public int countByExt(DhSettleExt dhSettleExt){
		return dhSettleMapper.countByExt(dhSettleExt);
	}
	
	@Override
	public DhSettle selectById(String id) {
		return dhSettleMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhSettleMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhSettle dhSettle) {
		return dhSettleMapper.countRefined(dhSettle);
	}

	@Override
	public DhSettle selectRefined(DhSettle dhSettle) {
		return dhSettleMapper.selectRefined(dhSettle);
	}
	
	@Override
	public int deletedRefined(DhSettle dhSettle) {
		return dhSettleMapper.deletedRefined(dhSettle);
	}

	@Override
	public boolean repeat(DhSettle dhSettle) {
		int count = countRefined(dhSettle);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhSettle.getId())) {
			return true;
		}
		DhSettle dbItem = selectRefined(dhSettle);
		if(null == dbItem) {
			return false;
		}
		return !dhSettle.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhSettleMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DhSettle> dhSettles) {
		return dhSettleMapper.insertBatch(dhSettles);
	}

	@Override
	public int countMultiByExt(YwBusinessQueryRequest ywBusinessQueryRequest) {

		return dhSettleMapper.countMultiByExt(ywBusinessQueryRequest);
	}

	@Override
	public List<YwBusiness> selectMultiByExt(
			YwBusinessQueryRequest ywBusinessQueryRequest) {
		List<YwBusiness> list=dhSettleMapper.selectMultiByExt(ywBusinessQueryRequest);
		return list;
	}
}
